/**
 * Contains custom JavaScript code
 */
var urlHolder = new Object();

function loadTableT() {
	$.get(urlHolder.records, function(response) {
		
 		$('#tableUsersT').find('tbody').remove();
 		
 		for (var i=0; i<response.users.length; i++) {
			var row = '<tr>';
			row += '<td><input type="radio" name="index" id="index" value="'+i+'"></td>';
			row += '<td>' + response.users[i].username + '</td>';
			row += '<td>' + response.users[i].firstName + '</td>';
			row += '<td>' + response.users[i].lastName + '</td>';
			row += '<td>' + getTrainingT(response.users[i].training) + '</td>';
			row += '</tr>';
	 		$('#tableUsersT').append(row);
 		}
 		
 		$('#tableUsersT').data('model', response.users);
 		toggleFormsT('hide'); ;
		
 	});
	//toggleFormsT('hide'); ;
}

function submitNewRecordT() {
	$.post(urlHolder.add, {
			username: $('#newUsernameT').val(),
			password: $('#newPasswordT').val(),
			firstName: $('#newFirstNameT').val(),
			lastName: $('#newLastNameT').val(),
			role: $('#newRoleT').val(),
			project :$('#newProjectT').val(),
			training :$('#newTrainingT').val()
		}, 
		function(response) {
			if (response != null) {
				loadTableT();
				toggleFormsT('hide'); ;
				toggleCrudButtonsT('show');
				alert('Success! Record has been added.');
			} else {
				alert('Failure! An error has occurred!');
			}
		}
	);	
}

function submitDeleteRecordT() {
	var selected = $('input:radio[name=index]:checked').val();
	
	$.post(urlHolder.del, {
			username: $('#tableUsersT').data('model')[selected].username
		}, 
		function(response) {
			if (response == true) {
				loadTableT(urlHolder.records);
				alert('Success! Record has been deleted.');
			} else {
				alert('Failure! An error has occurred!');
			}
		}
	);
}

function submitUpdateRecordT() {
	$.post(urlHolder.edit, {
			username: $('#editUsernameT').val(),
			firstName: $('#editFirstNameT').val(),
			lastName: $('#editLastNameT').val(),
			role: $('#editRoleT').val(),
			project:$('#editProjectT').val(),
			training:$('#editTrainingT').val()
		}, 
		function(response) {
			if (response != null) {
				loadTableT();
				toggleFormsT('hide'); ;
				toggleCrudButtonsT('show');
				alert('Success! Record has been edited.');
			} else {
				alert('Failure! An error has occurred!');
			}
		}
	);
}

function getTrainingT(training) {
	if (training == 21) {
		return 'Not Scheduled';
	} 
	else if (training == 1) {
		return 'T1';
	} 
	else if (training == 2) {
		return 'T2';
	} 
	else if (training == 3) {
		return 'T3';
	}
	else if (training == 4) {
		return 'T4';
	}
	else if (training == 5) {
		return 'T5';
	}
	else if (training == 6) {
		return 'T6';
	}
	else if (training == 7) {
		return 'T7';
	}
	else if (training == 8) {
		return 'T8';
	}
	else if (training == 9) {
		return 'T9';
	}
	else {
		return 'Unknown';
	} 
}

function hasSelectedT() {
	var selected = $('input:radio[name=index]:checked').val();
	if (selected == undefined) { 
		alert('Select a record first!');
		return false;
	}
	
	return true;
}

function fillEditFormT() {
	loadTrainingIds();
	var selected = $('input:radio[name=index]:checked').val();
	$('#editUsernameT').val( $('#tableUsersT').data('model')[selected].username );
	$('#editFirstNameT').val( $('#tableUsersT').data('model')[selected].firstName );
	$('#editLastNameT').val( $('#tableUsersT').data('model')[selected].lastName );
	$('#editRoleT').val( $('#tableUsersT').data('model')[selected].role );
}

function resetNewFormT() {
	$('#newUsernameT').val('');
	$('#newPasswordT').val('');
	$('#newFirstNamT').val('');
	$('#newLastNameT').val('');
	$('#newRoleT').val('2');
}

function resetEditFormT() {
	$('#editFirstNameT').val('');
	$('#editLastNameT').val('');
	$('#editRoleT').val('2');
}

function toggleFormsT(id) {
	
	if (id == 'hide') {
		$('#newFormT').hide();
		$('#editFormT').hide();
		
	} else if (id == 'new') {
 		resetNewFormT();
 		$('#newFormT').show();
 		$('#editFormT').hide();
 		
	} else if (id == 'edit') {
		
 		resetEditFormT();
 		$('#newFormT').hide();
 		$('#editFormT').show();
	}
}

function toggleCrudButtonsT(id) {
	if (id == 'show') {
		$('#newBtnT').removeAttr('disabled');
		$('#editBtnT').removeAttr('disabled');
		$('#deleteBtnT').removeAttr('disabled');
		$('#reloadBtnT').removeAttr('disabled');
		
	} else if (id == 'hide'){
		$('#newBtnT').attr('disabled', 'disabled');
		$('#editBtnT').attr('disabled', 'disabled');
		$('#deleteBtnT').attr('disabled', 'disabled');
		$('#reloadBtnT').attr('disabled', 'disabled');
	}
}

function loadTrainingIds() {
	

	$.get(urlHolder.popId, function(response) {
		$('#editTrainingT').val('');
		var list = null ;
		for (var i=0; i<response.trainings.length; i++) {
			list += '<option value='+(i+1)+'>'+response.trainings[i].trainingId+'</option>';
			//$('#editProjectP').append('<option value='+(i+1)+'>'+response.projects[i].projectId+'</option>');
		}
		$('#editTrainingT').html(list);
		});
	
}